/*
 * NormalInvoiceListItem.java
 * business
 *
 * Created by ChenTao on 2017/1/5.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.listview.displaylistitem;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.NormalInvoice;
import com.yidiandao.business.data.model.IInvoiceModel;
import com.yidiandao.business.data.model.InvoiceModel;
import com.yidiandao.business.ui.widget.dialog.EditNormalInvoiceDialog;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.viewholder.NormalInvoiceHolder;
import com.yidiandao.business.utils.UIUtils;

/** 普通发票ListItem */
public class NormalInvoiceListItem extends DisplayListItem<NormalInvoiceHolder, NormalInvoice>
        implements View.OnClickListener {

    private IInvoiceModel mInvoiceModel;

    private Activity mActivity;

    public NormalInvoiceListItem(NormalInvoice s) {
        super(s);
        mInvoiceModel = new InvoiceModel();
    }

    @Override
    public void onShow(Context context, NormalInvoiceHolder holder) {
        mActivity = (Activity) context;

        holder.refreshUI(data);

        // 编辑普通发票
        holder.tvEdit.setOnClickListener(this);
        // 删除普通发票
        holder.tvDelete.setOnClickListener(this);
        // 设置为默认发票
        holder.rootSetDefault.setOnClickListener(this);
    }

    @Override
    public String getDisplayItemTag() {
        return NormalInvoiceListItem.class.getSimpleName();
    }

    @Override
    public Class<NormalInvoiceHolder> getViewHolderClazz() {
        return NormalInvoiceHolder.class;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_delete:
                deleteNormalInvoice();
                break;
            case R.id.tv_edit:
                showEditNormalInvoiceDialog();
                break;
            case R.id.root_set_default:
                setDefaultInvoice();
                break;
        }
    }

    private void setDefaultInvoice() {

        if (data.isSelected()) {
            return;
        }

        // 设置为默认发票
        mInvoiceModel.setDefaultNormalInvoice(
                data.getInvId(),
                getDisplayItemTag(),
                new IInvoiceModel.OnNetAccessListener() {
                    @Override
                    public void onSuccess() {
                        UIUtils.showToast(UIUtils.getString(R.string.set_default_success));
                        mInvoiceModel.postRefreshNormalInvoiceEvent();
                    }

                    @Override
                    public void onFailed() {
                    }
                });
    }

    private void showEditNormalInvoiceDialog() {
        EditNormalInvoiceDialog dialog = new EditNormalInvoiceDialog(mActivity);
        dialog.show();
        dialog.setUpdateStyle(data.getInvTitle());
        dialog.setOnConfirmListener(
                new EditNormalInvoiceDialog.OnConfirmListener() {
                    @Override
                    public void onConfirm(String invoiceTitle) {
                        if (invoiceTitle.equals(data.getInvId())) {
                            UIUtils.showToast(UIUtils.getString(R.string.same_inv_title));
                            return;
                        }

                        updateNormalInvoice(invoiceTitle);
                    }
                });
    }

    private void updateNormalInvoice(String invoiceTitle) {
        mInvoiceModel.updateNormalInvoice(
                data.getInvId(),
                invoiceTitle,
                getDisplayItemTag(),
                new IInvoiceModel.OnNetAccessListener() {
                    @Override
                    public void onSuccess() {
                        UIUtils.showToast(
                                UIUtils.getString(R.string.update_normal_invoice_success));
                        mInvoiceModel.postRefreshNormalInvoiceEvent();
                    }

                    @Override
                    public void onFailed() {
                    }
                });
    }

    private void deleteNormalInvoice() {
        mInvoiceModel.deleteNormalInvoice(
                data.getInvId(),
                getDisplayItemTag(),
                new IInvoiceModel.OnNetAccessListener() {
                    @Override
                    public void onSuccess() {
                        UIUtils.showToast(UIUtils.getString(R.string.delete_success));
                        mInvoiceModel.postRefreshNormalInvoiceEvent();
                    }

                    @Override
                    public void onFailed() {
                    }
                });
    }
}
